package com.yunji.news.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunji.news.common.Result;
import com.yunji.news.entity.SystemConfig;
import com.yunji.news.service.SystemConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 系统配置管理控制器
 *
 * @author yunji
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/admin/system-configs")
@RequiredArgsConstructor
public class AdminSystemConfigController {

    private final SystemConfigService systemConfigService;

    /**
     * 分页查询配置列表
     */
    @GetMapping
    public Result<Page<SystemConfig>> getConfigPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String configGroup,
            @RequestParam(required = false) String configKey) {
        
        Page<SystemConfig> configPage = new Page<>(page, size);
        Page<SystemConfig> result = systemConfigService.getConfigPage(configPage, configGroup, configKey);
        return Result.success(result);
    }

    /**
     * 获取配置详情
     */
    @GetMapping("/{id}")
    public Result<SystemConfig> getConfigDetail(@PathVariable Long id) {
        SystemConfig config = systemConfigService.getById(id);
        if (config == null) {
            return Result.error("配置不存在");
        }
        return Result.success(config);
    }

    /**
     * 根据配置键获取配置值
     */
    @GetMapping("/value/{configKey}")
    public Result<String> getConfigValue(@PathVariable String configKey) {
        String value = systemConfigService.getConfigValue(configKey);
        return Result.successWithStringData(value);
    }

    /**
     * 根据配置分组获取配置列表
     */
    @GetMapping("/group/{configGroup}")
    public Result<List<SystemConfig>> getConfigsByGroup(@PathVariable String configGroup) {
        List<SystemConfig> configs = systemConfigService.getConfigsByGroup(configGroup);
        return Result.success(configs);
    }

    /**
     * 根据配置分组获取配置键值对
     */
    @GetMapping("/group/{configGroup}/map")
    public Result<Map<String, String>> getConfigMapByGroup(@PathVariable String configGroup) {
        Map<String, String> configMap = systemConfigService.getConfigMapByGroup(configGroup);
        return Result.success(configMap);
    }

    /**
     * 获取所有配置分组
     */
    @GetMapping("/groups")
    public Result<List<String>> getConfigGroups() {
        List<String> groups = systemConfigService.getConfigGroups();
        return Result.success(groups);
    }

    /**
     * 创建或更新配置
     */
    @PostMapping
    public Result<String> saveOrUpdateConfig(@Valid @RequestBody SystemConfig systemConfig) {
        boolean success = systemConfigService.saveOrUpdateConfig(systemConfig);
        if (success) {
            return Result.success("保存成功");
        }
        return Result.error("保存失败，配置键可能已存在");
    }

    /**
     * 更新配置
     */
    @PutMapping("/{id}")
    public Result<String> updateConfig(@PathVariable Long id, @Valid @RequestBody SystemConfig systemConfig) {
        systemConfig.setId(id);
        boolean success = systemConfigService.saveOrUpdateConfig(systemConfig);
        if (success) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    /**
     * 批量更新配置
     */
    @PutMapping("/batch")
    public Result<String> batchUpdateConfigs(@Valid @RequestBody List<SystemConfig> configs) {
        boolean success = systemConfigService.batchUpdateConfigs(configs);
        if (success) {
            return Result.success("批量更新成功");
        }
        return Result.error("批量更新失败");
    }

    /**
     * 删除配置
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteConfig(@PathVariable Long id) {
        boolean success = systemConfigService.deleteConfig(id);
        if (success) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败，该配置可能不可编辑");
    }

    /**
     * 刷新配置缓存
     */
    @PostMapping("/refresh-cache")
    public Result<String> refreshCache() {
        systemConfigService.refreshCache();
        return Result.success("缓存刷新成功");
    }
}